package nio.filechannel;

import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * 将缓冲区中数据写入文件
 */
public class NIOFileChannel1 {
    public static void main(String[] args) throws IOException {
        // 创建一个输出流
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\java-ee\\java-ee\\netty\\src\\test01.txt");
        FileChannel channel = fileOutputStream.getChannel();
        // 创建一个缓冲区 ByteBuffer
        ByteBuffer byteBuffer = ByteBuffer.allocate(20);
        byteBuffer.put("hello".getBytes());
        // 读写反转
        byteBuffer.flip();
        // 写入数据
        channel.write(byteBuffer);
        fileOutputStream.close();
    }
}
